<template>
	<view class="body">
		<view class="wrap">
			<u-form :model="userinfo"  :rules="rules" ref="uForm" :errorType="errorType">
				<u-form-item required  :leftIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="account" label-width="150" :label-position="labelPosition" label="姓名" prop="realname">
					<u-input  placeholder="请输入姓名" v-model="userinfo.realname" type="text"></u-input>
				</u-form-item>
				<u-form-item  required  :label-position="labelPosition" label="手机号码" prop="mobile" label-width="150">
					<u-input  placeholder="请输入手机号" v-model="userinfo.mobile" type="number"></u-input>
				</u-form-item>
			</u-form>
			<view class="pass">
				密码默认是123456
			</view>
		</view>
		
		<button type="default" class="submit" @click="submit">确定</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl:'',
				userinfo:{
					realname:"",
					mobile:""
				},
				labelPosition: 'left',
				border:false,
				errorType: ['message'],
				rules: {
					realname: [
						{
							required: true,
							message: '请输入姓名',
							trigger: 'blur' ,
						},
						{
							min: 2,
							max: 6,
							message: '姓名长度在2到6个字符',
							trigger: ['change','blur'],
						},
						{
							pattern: /^[\u4e00-\u9fa5]+$/gi,
							message: '姓名只能为中文',
							trigger: 'change',
						}
					],
					mobile: [
						{
							required: true,
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change','blur'],
						}
					]
			    }		
			}
		},
		onLoad() {
			 this.baseurl =  this.$u.http.config.baseUrl
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			//提交
			 submit() {
				let that  = this; 
				this.$refs.uForm.validate(valid => {
					if (valid) {
						that.$u.post('/api/member/register',that.userinfo
						).then(res => {
							//console.log(res);
							that.$u.toast("成功");
							setTimeout(function(){
								uni.navigateTo({
									url: '/pages/index/index'
								});
							},1000)
						}).catch(res=>{
							//console.log(res);
						});
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			 }
			
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #ededed;
	}
	.body{
		overflow: hidden;
	}
	.wrap{
		padding: 0 30rpx;
	}
	.pass{
		font-size: 24rpx;
		text-align: right;
		color: #bababa;
		margin-top: 50rpx;
	}
	.submit{
		width: 90%;
		//margin-left: -45%;
		transform: translateX(-50%);
		left:50%;
		line-height: 80rpx;
		background: #2165F7;
		border-radius: 44px;
		color: #fff;
		position: fixed;
		bottom: 100rpx;
		font-size: 30rpx;
	}
	
</style>
